(function() {
	'use strict';

	angular.module('cf-content.directive')

	.directive('classifyItem', ['$timeout', function($timeout) {
		return {
			restrict: 'AE',
			template:'<div  class="classify_item" ng-repeat="obj in itemlists">'
	    				+'<dd ng-click="activeItem(obj)" ng-class={"active":selectedId==obj.id} data-ng-if="!obj.editable"  ng-bind="obj.name"></dd>'
	    				+'<div class="editable_box">'
	    				+'<form class="form-buttons form-inline">'
	    					+'<input data-ng-model="obj.name" type="text" >'
	    					+'<input data-ng-model="obj.name" type="text">'
	    					+'<a class="btn btn-info" ng-click="save()">保存</a>'
	    					+'<a class="btn btn-default" ng-click="editableToggle(obj)">取消</a>'
	    				+'</form>'
	    			  +	'</div>'
	    				+'</div>',
			replace:true,
			controller:['$scope','$timeout',function(scope,$timeout){
				var timer=null;

				scope.editableToggle=function(obj){
					obj.editable=!obj.editable;
				}
				scope.activeItem=function(obj){
					if(timer)
						$timeout.cancel(timer)
					scope.selectedId=obj.id;
					timer=$timeout(function(){
						scope.load();
					},100,true)
				}
			}]
		}
	}])

	.directive('classifyItemBox', ['$timeout', function($timeout) {
		return {
			restrict: 'AE',
			template: '<dl><div class="classify_item">'
	    			+'<div class="editable_box" data-ng-show="addible">'
	    				+'<form class="form-buttons  text-center">'
	    				+'<input data-ng-model="insertdata.name" type="text" >'
	    				+'<div><a class="btn btn-info" ng-click="save()">保存</a>'
	    				+'<a class="btn btn-default" data-ng-click="toggleAdd()">取消</a></div>'
	    				+'</form>'
	    			+'</div>'
	    		+'</div><div classify-item></div></dl>',
			link:function(scope,ele){
				scope.toggleAdd=function(){
					scope.addible=!scope.addible;
				}

			}
		}
	}])

	.directive('classifyTool', ['$timeout', function($timeout) {
		return {
			restrict: 'AE',
			template: '<div class="classify">'
			    		+'<dt>{{::tooltitle}}<i><em class="glyphicon glyphicon-plus" data-ng-click="toggleAdd()">'
			    		+'</em><em class="glyphicon glyphicon-minus" data-ng-click="minus()"></em><em class="glyphicon glyphicon-pencil" data-ng-click="edit()">'
			    		+'</em></i></dt>'
						+'<div classify-item-box></div></div>',
			scope:{
				itemlists:'=',
				insertdata:'=',
				tooltitle:'=',
				addible:'=',
				selectedId:'=',
				wait:'=',
				minus:'&',
				edit:'&',
				save:'&',
				load:'&'
			},
			controller:['$scope','$timeout',function(scope,$timeout){
				console.log(scope)
				scope.toggleAdd=function(){
					scope.addible=!scope.addible;
				}
			}]
		}
	}])
	.directive('classifySelectItem', ['$timeout', function($timeout) {
		return {
			restrict: 'AE',
			template:'<div  class="classify_item" ng-repeat="obj in itemlists">'
	    				+'<dd ng-click="activeItem(obj)" ng-class={"active":selectedId==obj.id} data-ng-if="!obj.editable"  ng-bind="obj.name"></dd>'
	    				+'<div class="editable_box">'
	    				+'<form class="form-buttons form-inline">'
	    				+'<select data-ng-model="obj.name" ng-options="g.id as g.name for g in options"></select>'
	    					+'<a class="btn btn-info" ng-click="save()">保存</a>'
	    					+'<a class="btn btn-default" ng-click="editableToggle(obj)">取消</a>'
	    				+'</form>'
	    			  +	'</div>'
	    				+'</div>',
			replace:true,
			controller:['$scope','$timeout',function(scope,$timeout){
				var timer=null;
				scope.itemlists.sort(function(a,b){return a.id-b.id})
				scope.editableToggle=function(obj){
					obj.editable=!obj.editable;
				}
				scope.activeItem=function(obj){
					if(timer)
						$timeout.cancel(timer)
					scope.selectedId=obj.id;
					timer=$timeout(function(){
						scope.load();
					},100,true)
				}
			}]
		}
	}])

	.directive('classifySelectItemBox', ['$timeout', function($timeout) {
		return {
			restrict: 'AE',
			template: '<dl><div class="classify_item">'
	    			+'<div class="editable_box" data-ng-show="addible">'
	    				+'<form class="form-buttons form-inline">'
	    				+'<select data-ng-model="insertdata.id" ng-options="g.id as g.name for g in options"></select>'
	    				+'<a class="btn btn-info" ng-click="save()">保存</a>'
	    				+'<a class="btn btn-default" data-ng-click="toggleAdd()">取消</a>'
	    				+'</form>'
	    			+'</div>'
	    		+'</div><div classify-select-item></div></dl>',
			link:function(scope,ele){
				scope.$watch('options',function(newvalue){
					if(newvalue&&newvalue.length>0)
					scope.insertdata.id=newvalue[0].id;
				})
				scope.toggleAdd=function(){
					scope.addible=!scope.addible;
				}

			}
		}
	}])

	.directive('classifySelectTool', ['$timeout', function($timeout) {
		return {
			restrict: 'AE',
			template: '<div class="classify">'
			    		+'<dt>{{::tooltitle}}<i><em class="glyphicon glyphicon-plus" ng-if="save" data-ng-click="toggleAdd()">'
			    		+'</em><em class="glyphicon glyphicon-minus" data-ng-click="minus()" ng-if="save"></em>'
			    		+'</em></i></dt>'
						+'<div classify-select-item-box></div></div>',
			scope:{
				itemlists:'=',
				insertdata:'=',
				tooltitle:'=',
				addible:'=',
				selectedId:'=',
				wait:'=',
				options:'=',
				minus:'&',
				save:'&',
				load:'&'
			},
			controller:['$scope','$timeout',function(scope,$timeout){
				scope.toggleAdd=function(){
					scope.addible=!scope.addible;
				}
			}]
		}
	}])

})();
